package org.rsbot.script;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.rsbot.bot.Bot;
import org.rsbot.event.EventManager;
import org.rsbot.event.listeners.PaintListener;
import org.rsbot.script.methods.Game;
import org.rsbot.script.methods.MethodContext;
import org.rsbot.script.methods.Methods;
import org.rsbot.script.methods.Mouse;

public abstract class Random extends Methods
  implements PaintListener
{
  protected String name;
  private volatile boolean enabled = true;
  private Script script;
  private long timeout = random(240, 300);

  public abstract boolean activateCondition();

  public abstract int loop();

  public void onStart()
  {
  }

  public void onFinish()
  {
  }

  public long getTimeout()
  {
    return this.timeout;
  }

  public final void init(MethodContext paramMethodContext)
  {
    super.init(paramMethodContext);
    onStart();
  }

  public final boolean isActive() {
    return this.script != null;
  }

  public final boolean isEnabled() {
    return this.enabled;
  }

  public final void setEnabled(boolean paramBoolean) {
    this.enabled = paramBoolean;
  }

  protected void stopScript(boolean paramBoolean)
  {
    this.script.stopScript(paramBoolean);
  }

  public final void run(Script paramScript) {
    this.script = paramScript;
    this.name = ((ScriptManifest)getClass().getAnnotation(ScriptManifest.class)).name();
    paramScript.ctx.bot.getEventManager().removeListener(paramScript);
    for (Script localScript1 : paramScript.delegates) {
      paramScript.ctx.bot.getEventManager().removeListener(localScript1);
    }
    paramScript.ctx.bot.getEventManager().addListener(this);
    log("Random event started: " + this.name);
    long l = getTimeout();
    if (l > 0L) {
      l *= 1000L;
      l += System.currentTimeMillis();
    }
    while (true) if (paramScript.isRunning()) {
        try {
          int i = loop();
          if (i != -1)
          {
            if ((l > 0L) && (System.currentTimeMillis() >= l)) {
              this.log.warning("Time limit reached for " + this.name + ".");
              paramScript.stopScript();
            } else {
              sleep(i);
            }
          }
        } catch (Exception localException) {
          this.log.log(Level.SEVERE, "Uncatched exception: ", localException);
        }
      }

    this.script = null;
    onFinish();
    log("Random event finished: " + this.name);
    paramScript.ctx.bot.getEventManager().removeListener(this);
    sleep(1000);
    paramScript.ctx.bot.getEventManager().addListener(paramScript);
    for (Script localScript2 : paramScript.delegates)
      paramScript.ctx.bot.getEventManager().addListener(localScript2);
  }

  public final void onRepaint(Graphics paramGraphics)
  {
    Point localPoint = this.mouse.getLocation();
    int i = this.game.getWidth(); int j = this.game.getHeight();
    paramGraphics.setColor(new Color(0, 0, 0, 100));
    paramGraphics.fillRect(0, 0, localPoint.x - 1, localPoint.y - 1);
    paramGraphics.fillRect(localPoint.x + 1, 0, i - (localPoint.x + 1), localPoint.y - 1);
    paramGraphics.fillRect(0, localPoint.y + 1, localPoint.x - 1, j - (localPoint.y - 1));
    paramGraphics.fillRect(localPoint.x + 1, localPoint.y + 1, i - (localPoint.x + 1), j - (localPoint.y - 1));
  }
}